package com.xz.xzutils.module.xzmybatislogplus.socket.example;

import com.xz.xzutils.module.xzmybatislogplus.socket.*;

import java.util.stream.IntStream;

/**
 * <p>
 *
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025/3/22-03-22 16:21
 */
public class ClientExample {
    public static void main(String[] args) {
        SocketConfig config = new SocketConfig("localhost", 8888);
        config.setReconnectMaxNum(5);
        config.setCallback(new SocketCallback() {
            int i = 1;

            @Override
            public void onMessage(String message) {
                // System.out.println("[Client] Received: " + message + " " + i++);
            }

            @Override
            public String onMessageAndResponse(String message) {
                System.out.println("[Server] Received: " + message + " " + i++);
                // return "我是 Client，我已收到你的消息：" + message;
                return null;
            }

            @Override
            public void onError(Exception e) {
                System.err.println("[Client] Error: " + e.getMessage());
            }

            @Override
            public void onStatusChange(ConnectionStatus status) {
                System.out.println("[Client] Status: " + status);
            }
        });

        SocketEndpoint client = SocketFactory.createClient(config);
        client.start();


        // TODO
        // 现目前问题：server 重启后，client 不会自动重连，需要手动重启 client
        // 发送示例
        IntStream.range(0, 50).forEach(i -> {
            client.sendData("I am client " + i);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ignored) {
            }
        });
    }
}
